import load from 'lang-auto-js/data.json'
import keys from './modules/<%- language %>.json'

let getItem = key => {
  if (typeof uni != 'undefined') {
    return uni.getStorageSync(key)
  } else {
    return localStorage.getItem(key)
  }
}


let setItem = (key, value) => {
  if (typeof uni != 'undefined') {
    return uni.setStorageSync(key, value)
  } else {
    return localStorage.setItem(key, value)
  }
}
const mixin = {
  data() {
    return {

      keys: keys || {},
      language: getItem('language') || '<%- language %>',//当前语种
      languagemap: {},

    }
  },
  created() {
    this.Setlanguage(this.language)
  },
  computed: {
    languages() {
      let languages = []
      for (var name in load)
        languages.push(name)
      return languages
    }
  },
  methods: {
    //获取变量名
    generateUniqueVarName(multiLangText) {
      let keys = this.keys;
      let value = ''
      for (var key in keys) {
        if (keys[key] == multiLangText) {
          value = key
          break;
        }
      }
      return value;
    },
    async Setlanguage(language) {
      let path = load[language];
      let languages = await import(path);
      this.language = language
      setItem('language', language)
      for (var key in languages) {
        this.languagemap[key] = languages[key]
      }
    },
    $t(name) {
      let key = this.generateUniqueVarName(name)
      return this.languagemap[key]
    }
  }
}

export default {
  install(Vue) {
    Vue.mixin(mixin);
  }

}